package com.notary.tx_live_plugin.util;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import java.util.Arrays;

public class TxLiveUtils {

    /**
     * 无论成功与否都会回调{@link FragmentActivity#onRequestPermissionsResult(int, String[], int[])}方法
     * @param permission {@link android.Manifest.permission}
     */
    public static void requestPermission(FragmentActivity context, int requestCode, String... permission) {
        if(permission == null || context == null) return;
        for(String pre : permission) {
            if (ContextCompat.checkSelfPermission(context, pre) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(context, permission,requestCode);
                return;
            }
        }
        int[] result = new int[permission.length];
        Arrays.fill(result, PackageManager.PERMISSION_GRANTED);
        context.onRequestPermissionsResult(requestCode, permission, result);
    }

    public static void requestPermission(Activity context, int requestCode, String... permission) {
        if(permission == null || context == null) return;
        for(String pre : permission) {
            if (ContextCompat.checkSelfPermission(context, pre) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(context, permission,requestCode);
                return;
            }
        }
        ActivityCompat.requestPermissions(context, permission,requestCode);
    }

    /**
     * 无论成功与否都会回调{@link Fragment#onRequestPermissionsResult(int, String[], int[])}方法
     * @param permission {@link android.Manifest.permission}
     */
    public static void requestPermission(Fragment fragment, int requestCode, String... permission) {
        if(permission == null || fragment == null) return;
        Context context = fragment.getContext();
        if(context == null) return;
        for(String pre : permission) {
            if (ContextCompat.checkSelfPermission(context, pre) != PackageManager.PERMISSION_GRANTED) {
                fragment.requestPermissions(permission,requestCode);
                return;
            }
        }
        int[] result = new int[permission.length];
        Arrays.fill(result, PackageManager.PERMISSION_GRANTED);
        fragment.onRequestPermissionsResult(requestCode, permission, result);
    }

    /**
     *  检测权限申请是否可以
     */
    public static boolean isPermissionOK(String[] permission, int[] grantResults) {
        for(int i : grantResults) {
            if (i == PackageManager.PERMISSION_DENIED) {
                return false;
            }
        }
        return true;
    }

    /**
     * 将指定的秒数转换成00:00:00格式时间，例如：65转换后为00:01:05
     */
    public static String getFormatDuration(int duration) {
        int time = duration % 3600;
        int hour = duration / 3600;
        int minute = time / 60;
        int second = time % 60;
        StringBuilder builder = new StringBuilder();
        builder.append(String.format("%02d", hour)).append(':');
        builder.append(String.format("%02d", minute)).append(':');
        builder.append(String.format("%02d", second));
        return builder.toString();
    }

    /**
     * 获取网络连接的方式
     */
    public static int getNetworkStatus(Context context) {
        ConnectivityManager manager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo network = manager == null ? null : manager.getActiveNetworkInfo();
        if(network == null) return -1;
        return network.getType();
    }

    public static int getNetworkType(Context context){
        int type = getNetworkStatus(context);
        switch (type){
            case ConnectivityManager.TYPE_MOBILE:
            case ConnectivityManager.TYPE_MOBILE_DUN:
            case ConnectivityManager.TYPE_MOBILE_HIPRI:
            case ConnectivityManager.TYPE_MOBILE_MMS:
            case ConnectivityManager.TYPE_MOBILE_SUPL:
                return ConnectivityManager.TYPE_MOBILE;
            case ConnectivityManager.TYPE_WIFI:
                return ConnectivityManager.TYPE_WIFI;
        }
        return  -1;
    }
}
